<script setup>
import { ref, computed, watch, onMounted, onBeforeUnmount, nextTick, getCurrentInstance } from 'vue'
import { getDomain } from '@/api/login'
import useUserInfoStore from '@/store/userInfo'
import useCommonStore from '@/store/common'
const commonStore = useCommonStore()
const userStore = useUserInfoStore()
import { getStore } from '@/utils/store.js'
import warterMark from "@/utils/waterMark"
import icoImg from '@/assets/images/titleMg.png'
import { generateUUID } from '@/utils/util'
const isRouterAlive = ref(false)
let myInfo = ref({})
const channelBroad = ref(null)
const userAgent = ref(null)
const isClose = ref(false)
const isFinish = ref(false)
const showUpload = ref(true)
let uploadList = ref([])
const { $eventBus } = getCurrentInstance().appContext.config.globalProperties

// var scriptOnlyOffice = document.createElement('script')
// scriptOnlyOffice.type = 'text/javascript'
// scriptOnlyOffice.src = (window.location.protocol + pluginConfig.onlyOfficeUrl.substring(pluginConfig.onlyOfficeUrl.indexOf('://')+1))
// document.getElementsByTagName('head')[0].appendChild(scriptOnlyOffice);

const getDomainHandle = async () => {
  await getDomain().then(res => {
    if (res.data && res.data.code == 0) {
      if (res.data.data.systemName) {
        document.title = res.data.data.systemName
      }
      if (res.data.data) {
        userStore.setTenantInfo(res.data.data)
      } else {
        this.$router.push({ path: '/404' });
      }
    } else {
    }
  }).catch(err => {
  })
}

onMounted(() => {
  window.externalPath = '/jf-knowledge-plugins/PDFJS'
  window.global = {}
  window.id = generateUUID()
  myInfo.value = getStore({ name: "userInfo" });
  userAgent.value = navigator.userAgent;
  if ((userAgent.value.indexOf("Safari") == -1 || userAgent.value.indexOf("Chrome") >= 0) && typeof BroadcastChannel !== 'undefined') {
    channelBroad.value = new BroadcastChannel('upload-tree')
    channelBroad.value.addEventListener("message", receiveMessage, true)
  } else {
    addSendPostMessage()
  }
  nextTick(() => {
    var link = document.createElement('link')
    link.type = 'image/x-icon'
    link.rel = 'shortcut icon'
    link.href = ''
    if (myInfo.value) {
      if (myInfo.value?.tenant?.systemName) {
        document.title = myInfo.value.tenant.systemName
      }
      if (myInfo.value?.tenant?.icon) {
        link.href = myInfo.value.tenant.icon
      }
    } else {
      link.href = icoImg
    }
    document.getElementsByTagName('head')[0].appendChild(link);
    // 更新后给初始值
    isRouterAlive.value = true
    //用户名水印
    myInfo.value = getStore({ name: "userInfo" }) || {};
    if (myInfo.value.realName || myInfo.value.accountName) {
      warterMark.set(myInfo.value.realName || myInfo.value.accountName)
    } else {
      warterMark.clear()
    }
  })
})

const isPercentage = computed(() => {
  return uploadList.value.filter(item => { return item.percentage <= 100 })
})
const isUpload = computed(() => {
  return isPercentage.length > 0 && !isClose.value
})

function addSendPostMessage() {
  $eventBus.$on('upload-tree', (event) => {
    receiveMessage({ data: event })
  })
}

function receiveMessage(event) {
  myInfo = getStore({ name: "userInfo" }) || {};
  if (event.data.data.userId != myInfo.id) return
  if (isMobile_new) return
  isClose.value = false
  if (!isUpload) {
    showUpload.value = true
  }
  switch (event.data.type) {
    case "uploadFile":
      uploadList = event.data.data.fileList || []
      isFinish.value = event.data.data.isFinish || false
      showUpload.value = event.data.data.reStartUpload ? event.data.data.reStartUpload : showUpload.value
      break
    case 'uploadFileClose':
      if (isFinish.value) {
        isClose.value = true
      }
      showUpload.value = false
      break
    case 'default':
      console.log(event)
      break
  }
}

if (!userStore.tenantInfo) {
  getDomainHandle()
}
import zhCn from 'element-plus/dist/locale/zh-cn.mjs'
import en from 'element-plus/dist/locale/en.mjs'
const language = computed(() => (commonStore.lang === 'zh-CN' ? zhCn : en))
</script>

<template>
  <el-config-provider :locale="language">
    <div class="basic-cont-box">
      <router-view />
    </div>
  </el-config-provider>
</template>

<style scoped lang="scss">
.basic-cont-box {
  // padding: 8px 10px;
  position: relative;
  height: 100%;
  box-sizing: border-box;
}
</style>
